//
//  MDHCommonViewViewController.swift
//  JSTech
//
//  Created by 高得华 on 2019/5/8.
//  Copyright © 2020 华&梅. All rights reserved.
////  用于快速搭建 类似 设置界面的样式

import UIKit


////  用于快速搭建 类似 设置界面的样式
class MDHCommonViewViewController: MDHTableViewController {

    override var viewModel: MDHCommonViewModel {
        get {
            return super.viewModel as! MDHCommonViewModel
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
    
//    override var contentInset: UIEdgeInsets {
//        return UIEdgeInsets.zero
//    }
//    
//    override func viewDidLayoutSubviews() {
//        super.viewDidLayoutSubviews()
//        self.tableView?.mh_y = kMMWPTFC().kNavSafeArea
//        if self.hidesBottomBarWhenPushed {
//            self.tableView?.mh_height = self.view.mh_height-kMMWPTFC().kNavSafeArea-kMMWPTFC().kSafeAreaBottom
//        }else{
//            self.tableView?.mh_height = self.view.mh_height-kMMWPTFC().kNavSafeArea
//        }
//    }

    override func bindViewModel() {
        super.bindViewModel()
    }
    
    /*override func configureCell(_ cell: MDHTableViewCell, at indexPath: IndexPath, with object: Any) {
        cell.bindViewModel(object)
    }*/
    override func tableView(_ tableView: UITableView, dequeueReusableCellWithIdentifier identifier: String, for indexPath: IndexPath) -> UITableViewCell {
        return MDHCommonTableViewCell.initCell(tableView: tableView)
    }
    override func numberOfSections(in tableView: UITableView) -> Int {
        return self.viewModel.dataSource.count
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let groupViewModel = self.viewModel.dataSource[section] as? MDHCommonGroupViewModel ?? MDHCommonGroupViewModel()
        return groupViewModel.itemViewModels.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tableView(tableView, dequeueReusableCellWithIdentifier: "MDHTableViewCell", for: indexPath) as! MDHTableViewCell
        let groupViewModel = self.viewModel.dataSource[indexPath.section] as? MDHCommonGroupViewModel ?? MDHCommonGroupViewModel()
        let object = groupViewModel.itemViewModels[indexPath.row]
        object.indexWith(index: indexPath)
        cell.setIndexPath(indexPath: indexPath, rowsInSection: groupViewModel.itemViewModels.count)
        /// bind model
        self.configureCell(cell, at: indexPath, with: object)
        return cell
    }
    
    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footerView = MDHCommonFooterView.initHFView(tableView: tableView)
        let groupViewModel = self.viewModel.dataSource[section] as? MDHCommonGroupViewModel ?? MDHCommonGroupViewModel()
        footerView.bindViewModel(groupViewModel)
        return footerView
    }
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = MDHCommonHeaderView.initHFView(tableView: tableView)
        let groupViewModel = self.viewModel.dataSource[section] as? MDHCommonGroupViewModel ?? MDHCommonGroupViewModel()
        headerView.bindViewModel(groupViewModel)
        return headerView
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let groupViewModel = self.viewModel.dataSource[indexPath.section] as? MDHCommonGroupViewModel ?? MDHCommonGroupViewModel()
        let item = groupViewModel.itemViewModels[indexPath.row]
        return item.rowHeight
    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        let groupViewModel = self.viewModel.dataSource[section] as? MDHCommonGroupViewModel ?? MDHCommonGroupViewModel()
        return groupViewModel.headerHeight
    }
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        let groupViewModel = self.viewModel.dataSource[section] as? MDHCommonGroupViewModel ?? MDHCommonGroupViewModel()
        return groupViewModel.footerHeight
    }
}
